package com.opoc.controller;


import com.opoc.pojo.*;
import com.opoc.service.ChangeReviewService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 变更审核
 */
@Slf4j
@RestController
public class ChangeReviewController {
@Autowired
private ChangeReviewService changeReviewService ;

    /**
     * 5.1待审核变更请求查询-------(完成)
     */
    @GetMapping("/admin/request")
    public Result WaitingForReview(
            @RequestParam(value = "company_id", required = false) String company_id,
            @RequestParam(value = "company_name", required = false) String company_name,
            @RequestParam(value = "contacts", required = false) String contacts) {
        List<ChangeCheck> data = changeReviewService.WaitingForReview(company_id, company_name, contacts);
        return data.isEmpty() ? Result.error("is_null") : Result.success(data);
    }

    /**
     * 5.2已审核变更请求查询-----(完成)
     */
    @GetMapping("/admin/request/info")
    public Result ReviewedForChanges(
            @RequestParam(value = "company_id", required = false) String company_id,
            @RequestParam(value = "company_name", required = false) String company_name,
            @RequestParam(value = "contacts", required = false) String contacts) {
        List<ChangeRequest> data = changeReviewService.ReviewedForChanges(company_id, company_name, contacts);
        log.info(data.toString());
        return data.isEmpty() ? Result.error("is_null") : Result.success(data);
    }


    /**
     * 5.3已审核变更请求删除------(完成)
     */
//    有待商榷
    @DeleteMapping("/admin/request/info")
    public Result RequestDelete(@RequestParam("request_ids") List<Integer> request_ids){
        log.info(request_ids.toString());
        if(request_ids.isEmpty()){
            return Result.error("is_null");
        }else {
            changeReviewService.Delete(request_ids);
            log.info(request_ids.toString());
            return Result.success();
        }
    }


    /**
     * 5.4待审核变更请求批复-------(完成)
     */
    @PostMapping ("/admin/request/info")
    public Result RequestDelete1(@RequestParam("request_id") Integer request_id,
                                 @RequestParam("auditor_id") String auditor_id,
                                 @RequestParam("auditor_result") int auditor_result){
            //  如果均不为空，则实现方法
        if(request_id!= null && auditor_id!= null){
            changeReviewService.change(request_id, auditor_id, auditor_result);
            return Result.success();
        }else {
            return Result.error("is_null");
        }
    }


    /**
     * 5.5发起变更请求-------(完成)
     */
    @PostMapping("/user/request")
    public Result ChangeRequest(@RequestParam("company_id") String company_id,
                                @RequestParam( "change_properties") Integer change_properties,
                                @RequestParam("new_value") String new_value,
                                @RequestParam("evidence_file") MultipartFile evidence_file){
        log.info("this:{},{},{}",company_id, change_properties,new_value);
        changeReviewService.update(company_id, change_properties, new_value, evidence_file);
        return Result.success();
    }
//    //上传佐证材料
//    @PostMapping("/user/request/file")
//    public Result evidence_fileByid(@RequestParam("company_id") String company_id,
//                                    @RequestParam("evidence_file") MultipartFile evidence_file){
//        log.info("{}{}",company_id, evidence_file);
//        if(company_id.isEmpty() || evidence_file.isEmpty()){
//            return Result.error("is_null");
//        }else {
//            changeReviewService.evidence_fileByid(company_id, evidence_file);
//            return Result.success();
//        }
//    }


    /**
     * 5.6发起检测报告变更请求
     */
//    @PutMapping("/user/request/report")
//    public Result changePoolRequest(@RequestBody PoolChange poolchange){
//        log.info("有这些参数：{}", poolchange);
//        changeReviewService.changePool(poolchange);
//        return Result.success();
//    }

    @PostMapping("/user/request/report")
    public Result changePoolRequest(@RequestParam("company_id") String company_id,
                                    @RequestParam("lhq_top") Double lhq_top,
                                    @RequestParam("lhq_bottom") Double lhq_bottom,
                                    @RequestParam("co_top") Double co_top,
                                    @RequestParam("co_bottom") Double co_bottom,
                                    @RequestParam("gas_top") Double gas_top,
                                    @RequestParam("gas_bottom") Double gas_bottom,
                                    @RequestParam("test_date") String test_date,
                                    @RequestParam("test_organization") String test_organization,
                                    @RequestParam("report_file") MultipartFile report_file
                                    ){
        log.info( "有这些参数：{},{},{},{},{},{},{},{},{},{}", company_id, lhq_top, lhq_bottom, co_top, co_bottom, gas_top, gas_bottom, test_organization, test_date, report_file);
        changeReviewService.changePool( company_id, lhq_top, lhq_bottom, co_top, co_bottom, gas_top, gas_bottom, test_organization, test_date, report_file);
        return Result.success();
    }

}
